package com.feeyo.monitor.service.imp;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.feeyo.monitor.mapper.DepartmentMapper;
import org.apache.commons.lang3.StringUtils;
import org.postgresql.util.PGobject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.feeyo.monitor.cache.CacheCons;
import com.feeyo.monitor.cache.CacheManager;
import com.feeyo.monitor.domain.UserVehiclePermission;
import com.feeyo.monitor.mapper.UserVehiclePermissionMapper;
import com.feeyo.monitor.service.UserVehiclePermissionService;

@Service
public class UserVehiclePermissionServiceImp implements UserVehiclePermissionService {

	@Autowired
	UserVehiclePermissionMapper userVehiclePermissionMapper;
    @Autowired
    DepartmentMapper departmentMapper;

	@Autowired
	CacheManager cacheManager;
	
	@Override
	public int insert(UserVehiclePermission record) {
		return userVehiclePermissionMapper.insert(record);
	}

    @Override
    public int insertSelective(UserVehiclePermission record) {
        return userVehiclePermissionMapper.insertSelective(record);
    }

    @Override
	public UserVehiclePermission selectByPrimaryKey(Integer id) {
		return userVehiclePermissionMapper.selectByPrimaryKey(id);
	}

	@Override
	public Map<Object,Object> selectByUserId(String userId) {
		
		Map<Object, Object> cacheResult = new HashMap<>();//cacheManager.hashGetEntire(CacheCons.USER_PERMMISIONS + userId);
		
		if (cacheResult.isEmpty()){
			Map<String,Object> result = userVehiclePermissionMapper.selectByUserId(userId);
			PGobject obj = (PGobject)result.get("permissions");
			JSONObject json = (JSONObject) JSON.parse(obj.toString());
			if (json.getString("airport").equals("*")){
				cacheManager.hashPut(CacheCons.USER_PERMMISIONS + userId, "airport", "NONE");

			}else{
				JSONArray jsonArr = JSON.parseArray(json.getString("airport"));
				cacheManager.hashPut(CacheCons.USER_PERMMISIONS + userId, "airport", StringUtils.join(jsonArr,","));
			}

			if (json.getString("company").equals("*")){
				cacheManager.hashPut(CacheCons.USER_PERMMISIONS + userId, "company", "NONE");
			}else{
				JSONArray jsonArr = JSON.parseArray(json.getString("company"));
				cacheManager.hashPut(CacheCons.USER_PERMMISIONS + userId, "company", StringUtils.join(jsonArr,","));
			}

			if (json.getString("department").equals("*")){
				cacheManager.hashPut(CacheCons.USER_PERMMISIONS + userId, "department", "NONE");
			}else{
			    String deptstring = json.getString("department");
				JSONArray jsonArr = JSON.parseArray(json.getString("department"));
				ArrayList<String> tempList = new ArrayList<String>();
				jsonArr.forEach(t -> {
				        if(!t.toString().equals("")) {
                            List<String> depts = departmentMapper.selectTreeNode(t.toString());
                            String deptstr = StringUtils.join(depts, ",");
                            tempList.add(deptstr);
                        }
					}
				);
//                StringBuffer sb = new StringBuffer();
//                jsonArr.forEach(t ->{
//                    List<String> depts = departmentMapper.selectTreeNode(t.toString());
//                    String deptstr = StringUtils.join(depts,",");
//                    sb.append(deptstr);
//                });

				cacheManager.hashPut(CacheCons.USER_PERMMISIONS + userId, "department", StringUtils.join(tempList.toArray(),","));
			}
			if (json.getString("vehicletype").equals("*")){
				cacheManager.hashPut(CacheCons.USER_PERMMISIONS + userId, "vehicletype", "NONE");
			}else{
				JSONArray jsonArr = JSON.parseArray(json.getString("vehicletype"));
				cacheManager.hashPut(CacheCons.USER_PERMMISIONS + userId, "vehicletype", StringUtils.join(jsonArr,","));
			}
			cacheResult = cacheManager.hashGetEntire(CacheCons.USER_PERMMISIONS + userId);
            cacheResult.put("id",result.get("id"));
            cacheResult.put("monitorVisit",json.getBoolean("monitorVisit"));
            cacheResult.put("limitVisit",json.getBoolean("limitVisit"));
            cacheResult.put("limitEdit",json.getBoolean("limitEdit"));
            cacheResult.put("runStat",json.getBoolean("runStat"));
            cacheResult.put("alarm",json.getBoolean("alarm"));
            cacheResult.put("vehicleManager",json.getBoolean("vehicleManager"));
            cacheResult.put("deptManager",json.getBoolean("deptManager"));

            cacheResult.put("vehicleEdit",json.getBoolean("vehicleEdit"));
            cacheResult.put("deptEdit",json.getBoolean("deptEdit"));
            cacheResult.put("userManager",json.getBoolean("userManager"));
            cacheResult.put("userEdit",json.getBoolean("userEdit"));
            cacheResult.put("alarmDelete",json.getBoolean("alarmDelete"));
		}


		return cacheResult;
	}

    @Override
    public String selectDeptByUserId(String userId) {
        Map<String,Object> result = userVehiclePermissionMapper.selectByUserId(userId);
        PGobject obj = (PGobject)result.get("permissions");
        JSONObject json = (JSONObject) JSON.parse(obj.toString());

        if (json.getString("department").equals("*")){
            return "*";
        }else {
            JSONArray jsonArr = JSON.parseArray(json.getString("department"));
            return jsonArr.get(0).toString();
        }

    }

    @Override
    public int updateByPrimaryKeySelective(UserVehiclePermission record) {
        return userVehiclePermissionMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateDeleteStatus(String id) {
        return userVehiclePermissionMapper.updateDeleteStatus(id);
    }

}
